const rpc_logger = require('pomelo-logger').getLogger('con-log', __filename);

module.exports = function() { return new Filter(); }
const Filter = function() { };
let startTime = null;   //开始请求时间

Filter.prototype.before = function (msg, session, next) {
  startTime = new Date().valueOf();
  next();
};

Filter.prototype.after = function (err, msg, session, resp, next) {
  let {__route__, ...req} = msg;
  //处理请求的间隔时间
  let intervalTime = new Date().valueOf() - startTime;
  if(err){
    rpc_logger.error(`[${__route__}] [${JSON.stringify(req)}] [${intervalTime} ms] ${err.stack}`);
  }else{
    rpc_logger.info(`[${__route__}] [${JSON.stringify(req)}] [${intervalTime} ms] ${JSON.stringify(resp)}`);
  }
  next(err);
};
